package com.gsnd.briuppts.service.impl;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsnd.briuppts.entity.DO.ManagerFenceHouseExtend;
import com.gsnd.briuppts.entity.ManagerFenceHouse;
import com.gsnd.briuppts.mapper.ManagerFenceHouseMapper;
import com.gsnd.briuppts.service.ManagerFhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ManagerFhServiceImpl implements ManagerFhService {

    @Autowired
    ManagerFenceHouseMapper managerFenceHouseMapper;

    @Override
    public PageInfo<ManagerFenceHouse> findAllByConditionByPage(String fhName, Integer pageNum, Integer pageSize) {
        // 1、开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 2、获得数据
        List<ManagerFenceHouse> managerFenceHouses = managerFenceHouseMapper.selectAllByCondition(fhName);
        // 3、创建PageInfo对象，然后将需要分页的数据放到构造器里即可
        PageInfo<ManagerFenceHouse> pageInfo = new PageInfo<>(managerFenceHouses);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public void addAndEdit(ManagerFenceHouse managerFenceHouse) {
        // 判断一下，到底是新增还是修改？
        // 根据数据对象里是否有fhId进行判断
        // 有就是更新，没有就是新增
        String fhId = managerFenceHouse.getFhId();
        // 引用工具类去判断fhId是否存在
        if(StrUtil.isEmpty(fhId)){
            // fhId不存在，新增
            // 注意id的生成策略（雪花算法）
            SnowflakeGenerator generator = new SnowflakeGenerator();
            Long snowId = generator.next();
            String fhNewId = String.valueOf(snowId);
            managerFenceHouse.setFhId(fhNewId);
            managerFenceHouse.setFhDelete(0);
            managerFenceHouseMapper.insert(managerFenceHouse);
        }else{
            // fhId存在，更新，但是不更新null值
            managerFenceHouseMapper.updateSelective(managerFenceHouse);
        }

    }

    @Override
    public void removeByIdOrAll(List<String> fhIds) {
        managerFenceHouseMapper.logicDeleteBatch(fhIds);
    }

    @Override
    public ManagerFenceHouseExtend findDetailByFhId(String fhId) {
        return managerFenceHouseMapper.selectDetailById(fhId);
    }
}
